﻿using System;
using System.Web;
using System.IO;

namespace SyncServer
{
    public class Print : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            String ResponseData = "";
            try
            {
                HttpResponse response = context.Response;
                byte[] requestData = new byte[context.Request.ContentLength];
                int length = context.Request.InputStream.Read(requestData, 0, requestData.Length);
                length = requestData.Length;
                string RequestContent = System.Text.Encoding.UTF8.GetString(requestData, 0, length);
                string Folder = System.Configuration.ConfigurationManager.AppSettings["PrintFilePath"].ToString();
                if (!Directory.Exists(Folder))
                {
                    Directory.CreateDirectory(Folder);
                }
                if (!String.IsNullOrEmpty(RequestContent))
                {
                    File.WriteAllText(Folder + "\\" + context.Request.QueryString.ToString(), RequestContent);
                    ResponseData = "OK";
                }
                else
                {
                    ResponseData = "上传的数据长度为0，无法进行保存";
                }
            }
            catch(Exception err)
            {
                ResponseData = err.StackTrace;
            }
            context.Response.ContentType = "text/plain";
            //获取请求内容并保存

            context.Response.Write(ResponseData);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}